package com.monitor.service.impl;

import com.github.pagehelper.PageHelper;
import com.monitor.common.page.PageBean;
import com.monitor.common.response.ResponseFactory;
import com.monitor.common.response.Result;
import com.monitor.dao.TaskInfoDao;
import com.monitor.dao.TaskInfoMapper;
import com.monitor.pojo.TaskInfo;
import com.monitor.service.TaskInfoService;
import com.monitor.task.config.QuartzManager;
import com.monitor.task.emunration.EnumTaskEnable;
import com.monitor.vo.TaskVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @ClassName TaskInfoServiceImpl
 * @Package com.monitor.service.impl
 * @Description TODO
 * @Author zhixingbai
 * @Create 2018/10/1517:21
 */
@Service
public class TaskInfoServiceImpl implements TaskInfoService {

    @Autowired
    private TaskInfoDao taskInfoDao;
    @Autowired
    private TaskInfoMapper taskInfoMapper;
    @Autowired
    private QuartzManager quartzManager;

    @Override
    public PageBean<TaskInfo> selectTaskList(TaskVo taskVo) {
        PageHelper.startPage(taskVo.getPageNum(), taskVo.getPageSize());
        List<TaskInfo> list = taskInfoDao.selectTaskList();
        return new PageBean<>(list);
    }

    @Override
    public Result editTask(TaskInfo taskInfo) {
        taskInfo.setCreateTime(new Date());
        taskInfo.setUpdateTime(new Date());
        taskInfoMapper.updateByPrimaryKeySelective(taskInfo);
        if(EnumTaskEnable.STOP.getCode().equals(taskInfo.getEnable())){
            quartzManager.deleteJob(taskInfo.getJobName());
        }else{
            quartzManager.updateJob(taskInfo.getJobName(),taskInfo.getCron());
        }
        return ResponseFactory.build();
    }
}
